<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title></title>
		<link rel="stylesheet" type="text/css" href="../../../resource/css/bootstrap.min.css"/>
		<link rel="stylesheet" type="text/css" href="../../../resource/css/jedate.css"/>
		<link rel="stylesheet" type="text/css" href="../../../resource/fonts/iconfont.css"/>
		<!--<link rel="stylesheet" type="text/css" href="../../../resource/css/ui-select.css"/>-->
		<link rel="stylesheet" type="text/css" href="../../../resource/css/common.css"/>
		<style>
			.erp-details table{text-align: center;}
			.erp-details table tr th{width: 30%;text-align: center;}
			table {font-family: verdana,arial,sans-serif;font-size:11px;color:#333333;border-width: 1px;border-color: #666666;  border-collapse: collapse;}
			table th {border-width: 1px;padding: 8px;border-style: solid;border-color: #666666; background-color: #dedede;}
			table td {border-width: 1px;padding: 8px;border-style: solid;border-color: #666666;background-color: #ffffff;}
		</style>
	</head>
	<body>
		<div class="container-fluid">
			<div class="row mheader">
				<div class="col-xs-2 text-left"><a href="javascript:;" onclick="javascript:history.back(-1);"><span class="iconfont icon-fanhui"></span></a></div>
				<div class="col-xs-8"><h2>材料供货单供货审核</h2></div>
				<div class="col-xs-2 text-right"> <a href="#" onclick="tijiao()">提交</a> </div>
			</div>
			<div class="row">
				<dl class="erp-list erp-details">
					<dd>
						<div class="erp-txt visa-txt">
						</div>
					</dd>
				</dl>
				<dl class="erp-list erp-details">
					<dd>
						<table>
							<thead>
								<tr>
									<th>材料类型</th>
									<th>是否超出</th>
									<th>超出/剩余材料量</th>
								</tr>
							</thead>
							<tbody>
								<!--<tr>-->
									<!--<td></td>-->
									<!--<td></td>-->
									<!--<td></td>-->
								<!--</tr>-->
							</tbody>
						</table>
					</dd>
				</dl>
				<dl class="erp-order material-order">
					<dt>材料供货单供货明细</dt>
				</dl>
			</div>
		</div>
	</body>
</html>
<script src="https://cdn.bootcss.com/jquery/2.2.4/jquery.min.js"></script>
<script src="../../../resource/js/bootstrap.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../resource/js/ui-select.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../resource/js/style.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
	//点击改变申请单高度
	$(document).ready(function(){
		  $(".more").click(function(){
		    $(this).siblings(".order-li").toggleClass("orderHeight");
		  });
	});
    $(function(){
        load();
    })
	//获取主键参数
    var remark=window.location.href.split("?")[1];
    //获取json数据的个数
	var ge=0;
    //获取数据
    function load(){
        ajaxs('api/matperson/loadghMoudle.do','post',{type:'c',remark:remark,cc_uuid: sessionStorage.ccuids,p:getP()},			function(data){
                console.log(data)
                var json=data.data;
                console.log(json)
                ge=json.length;
                //渲染主要数据
                $('.erp-txt').append('<p><span class="sp">项目名称:</span><span class="divCon">'+json[0].proName+'</span></p>' +
                    '<p><span class="sp">施工单位:</span><span class="divCon">'+json[0].compName1+'</span></p>' +
                    '<p><span class="sp">收货地址:</span><span class="divCon">'+json[0].r_adr+'</span></p>' +
                    '<p><span class="sp">供货时间:</span><span class="divCon">'+json[0].rCreateTime+'</span></p>');
                //判断是否显示审核按钮
					if(json[0].rState !=-1||json[0].rValidate==1){
						$('.text-right').hide();
					}else{
						$('.text-right').show();
					}
					var xiaoji=0;

                    for(var i=0;i<json.length;i++){
                    for(var i=0;i<json.length;i++){
                        json[i].mat_Bit=='根'?xiaoji=json[i].old_Price*json[i].fin_Num*6:xiaoji=json[i].old_Price*json[i].fin_Num;
                        $('.erp-order').append('<dd>' +
                            '<p class="zhujianid" isd="'+json[i].id+'"><span>材料名称:</span>'+json[i].mat_Name+'</p>' +
                            '<div class="order-li clearfix">' +
                            '<div class="col-xs-7"><span>材料规格:</span>'+json[i].mat_Spec+'</div>' +
                            '<div class="col-xs-5"><span>单价:</span>'+json[i].old_Price+'</div>' +
                            '<div class="col-xs-7"><span>计量单位:</span>'+json[i].mat_Bit+'</div>' +
                            '<div class="col-xs-5"><span>送料数量:</span>'+json[i].send_Num+'</div>' +
                            '<div class="col-xs-7"><span>录入人员:</span>'+json[i].rCreateUser+'</div>' +
                            '<div class="col-xs-5"><span>小计:</span>'+xiaoji+'</div>' +
                            '<div class="col-xs-12 clearfix">' +
                            '<span>审核数量:</span>' +
                            '<input type="text" class="mD-num nums" style="margin-bottom:5px;" name="" id="" value="'+json[i].send_Num+'" />' +
                            '</div>' +
                            '<div class="col-xs-12 clearfix">' +
                            '<span>质量审核:</span>' +
                            '<select class="sel_0">' +
                            '          <option value="0">合格</option>' +
                            '          <option value="1">不合格</option>' +
                            '        </select>' +
                            '</div>' +
                            '<div class="col-xs-12 clearfix">' +
                            '<span>备注:</span>' +
                            '<input type="text" class="mD-num beizhu" name="" id="" value="正常" />' +
                            '</div>' +
                            '</div>' +
                            '</dd>')
                    }




                }

				var arrs1=[];
				var k = 0, j = 0;
				var nums;
				var material={};
			console.log(data.fullDown.message[0])
            $(data.fullDown.message[0]).each(function (i,item) {
                var key = item.cc_key;
                var value = item.cc_value;


                if ( key.indexOf("直") > 0 || key.indexOf("梅") > 0 ){
                    var z=/(\d*)[^\d]*/ig;
                    var zongnums=0;

                    nums=key.replace(/[^0-9]+/g, ',');
                    nums=nums.substring(0,nums.length-1);
                    nums=nums.split(',');
                    for(var i=0;i<nums.length;i++){
                        zongnums+=Number(nums[i]);
                    }
                    console.log(z.exec(key),'||')
                    //km情况
                    value = zongnums * parseFloat(value) * 1000 ; // m
                    k += value;
                }else {
                    value = parseFloat(value); //座
                    j += value;
                }
            });
            var d = {"kong":k,"j":j};
            console.log(d,">>>>>>>>>>>>>>>>>>>>>>>")
            //拿细项的
            var kaicao;
            var renshou;
            $(data.fullDown.message[1]).each(function (i,item) {
                var key = item.mat_Bit.toString().toLowerCase();
                var value = parseFloat( item.allNum );
                var overflowNum=0;

                if ( key.indexOf("根")!=-1 || key.indexOf( "m" )!=-1 ||key.indexOf( "米" )!=-1 || key.indexOf( "km" )!=-1  || key.indexOf("公里")!=-1 ){
                    var vali = 0; //0代表够,1代表超出
                    if ( key.indexOf("根")!=-1 ){
                        vali = d.kong - ( value * 6 ) > 0 ? 0 : 1;
                        overflowNum=d.kong - value ;
                        console.log(overflowNum,'1');

                    }else if ( key.indexOf( "m" )!=-1 ){
                        vali = d.kong > value ? 0 : 1;
                        overflowNum=d.kong-value;
                        console.log(overflowNum,'2');

                    }else if ( key.indexOf( "km" ) || key.indexOf("公里") ){
                        vali = d.kong - ( value * 1000 ) > 0 ? 0 : 1;
                        overflowNum=d.kong - ( value * 1000 );
                        console.log(overflowNum,'3');

                    }else if(key.indexOf( "米" )!=-1){
                        vali = d.kong > value ? 0 : 1;
                        overflowNum=d.kong-value;
                        console.log(overflowNum,'4');
                    }
                    kaicao=vali;
                    console.log(item.p_name,"  是否超出：  ",kaicao);
                    if(kaicao==1){
                        arrs1.push(item.mat_Type);
                    }
                    var a=item.p_name;
                    material[a+'('+item.mat_Bit+')']=[kaicao,overflowNum];

                }else {
                    //0代表够,1代表超出
                    var vali = k.j > value ? 0 : 1;
                    renshou=vali;
                    if(renshou==1){
                        arrs1.push(item.mat_Type);
                    }
                    console.log(item.p_name," 是否超出：  ",renshou);
                    var a=item.p_name;
                    material[a]=[renshou,overflowNum];


                }

                console.log(material,'?>?>?>?>?>?>?>');

                console.log(arrs1,'?>?>?>?>?>?>?>');


            });
            for(var key in material){
                console.log(material[key])
                var d=material[key][0];
                d==0?d='未超出':d='已超出';
                $('.erp-details tbody').append('<tr>\n' +
                    '                            <td>'+key+'</td>\n' +
                    '                            <td>'+d+'</td>\n' +
                    '                            <td>'+Math.abs(material[key][1])+'</td>\n' +
                    '                        </tr>')



            }


		})
    }
    //提交数据
	function tijiao(){
		var paramStr="";
        //遍历获取详细内容
		for(var i=0;i<ge;i++){

            //供货单id主键列
            var id=$('.zhujianid').eq(i).attr('isd');
            //审批数量
            var sl=$('.nums').eq(i).val();
            //审批结果
			var result=$('.sel_0').eq(i).val();
            //备注
			var mark=$('.beizhu').eq(i).val();
            paramStr+=id+","+sl+","+result+","+mark+"|";
		}
		paramStr=paramStr.substring(0,paramStr.length-1);
		console.log('paramStr',paramStr)
        ajaxs(
            'api/buil/builGhVali.do',
			'post',
			{
			    //供货单id主键列,审批数量，审批结果，备注
                //paramStr:153,51,1,正常1|154,171,0,正常
                paramStr:paramStr,
				//供料单单号
                rm_code:remark,
        		p:getP()
			},
			function(data){
                console.log(data)
                window.tool.alert(data.msg);
				window.location.href="./erp-list.html";
			}
		)
	}
</script>











